package com.algrithom.math;

import java.util.Arrays;

/**
 * 238. 除自身以外数组的乘积
 */
public class Solution7 {
    
    public static void main(String[] args){
        //        int[] matrics = {0,0};
        int[] matrics = {0,1,2,0};
        //        int[] matrics = {1,0};
        System.out.println(Arrays.toString(productExceptSelf(matrics)));
    }
    
    private static int[] productExceptSelf(int[] nums){
        
        int length = nums.length;
        
        int[] L = new int[length];
        int[] R = new int[length];
        
        int[] answer = new int[length];
        
        L[0] = 1;
        for (int i = 1; i < length; i++) {
            L[i] = nums[i - 1] * L[i - 1];
        }
        
        R[length - 1] = 1;
        for (int i = length - 2; i >= 0; i--) {
            R[i] = nums[i + 1] * R[i + 1];
        }
        
        for (int i = 0; i < length; i++) {
            answer[i] = L[i] * R[i];
        }
        return answer;
    }
}
